//
//  DropDownListView.m
//  DropDownDemo
//
//  Created by 童明城 on 14-5-28.
//  Copyright (c) 2014年 童明城. All rights reserved.
//

#import "DropDownListView.h"
#import "StyledTableViewCell.h"
#define DEGREES_TO_RADIANS(angle) ((angle)/180.0 *M_PI)
#define DEGREES_TO_RADIANS_BAN(angle) ((angle)/180.0 *M_PI)
#define RADIANS_TO_DEGREES(radians) ((radians)*(180.0/M_PI))
#define HeighCell 35
@implementation DropDownListView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame dataSource:(id)datasource delegate:(id) delegate Tag:(NSString *)tag
{

    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];
        self.frame = frame;
        currentExtendSection = -1;
        self.dropDownDataSource = datasource;
        self.dropDownDelegate = delegate;
        
        NSInteger sectionNum =0;
        if ([self.dropDownDataSource respondsToSelector:@selector(numberOfSections)] ) {
            
            sectionNum = [self.dropDownDataSource numberOfSections];
        }
        
        if (sectionNum == 0) {
            self = nil;
       }
    }
    return self;
}
-(void)closeListdepart
{
    UIImageView *currentIV= (UIImageView *)[self viewWithTag:(SECTION_IV_TAG_BEGIN +currentExtendSection)];
    [UIView animateWithDuration:0.3 animations:^{
        if(self.tag == 0){
            CGRect r = currentIV.frame;
            r.origin.y = r.origin.y + 5;
            currentIV.frame = r;
            currentIV.transform = CGAffineTransformRotate(currentIV.transform, DEGREES_TO_RADIANS_BAN(-90));
        }
        else if(self.tag == 1){
            currentIV.transform = CGAffineTransformRotate(currentIV.transform, DEGREES_TO_RADIANS(180));
        }
        
    }];
   // [self.dropDownDataSource numberOfRowsInSection:-1];
    if(self.tag == 0)
    [self.dropDownDelegate moveViewBtn:0];
    [self hideExtendedChooseView];
}
-(void)sectionBtnTouch:(UIButton *)btn
{
    NSInteger section = btn.tag - SECTION_BTN_TAG_BEGIN;
    currentExtendSection = section;
    [self showChooseListViewInSection:currentExtendSection choosedIndex:[self.dropDownDataSource defaultShowSection:currentExtendSection]];
}

- (void)setTitle:(NSString *)title inSection:(NSInteger) section
{
    UIButton *btn = (id)[self viewWithTag:SECTION_BTN_TAG_BEGIN +section];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = Font(16);
    btn.titleLabel.textColor = [Common colorFromHexRGB:@"333333"];
}

- (BOOL)isShow
{
    if (currentExtendSection == -1) {
        return NO;
    }
    return YES;
}
-  (void)hideExtendedChooseView
{
    self.isOpen = NO;
    if (currentExtendSection != -1) {
        currentExtendSection = -1;
        CGRect rect = self.mTableView.frame;
        rect.size.height = 0;
        [UIView animateWithDuration:0.4 animations:^{
            self.mTableBaseView.alpha = 1.0f;
            self.mTableView.alpha = 1.0f;
            
            self.mTableBaseView.alpha = 0.2f;
            self.mTableView.alpha = 0.2;
            
            self.mTableView.frame = rect;
        }completion:^(BOOL finished) {
            [self.mTableView removeFromSuperview];
            [self.mTableBaseView removeFromSuperview];
        }];
    }
}

-(void)showChooseListViewInSection:(NSInteger)section choosedIndex:(NSInteger)index
{
    self.isOpen = YES;
    CGRect r = self.frame;
    if (!self.mTableView) {
        self.mTableBaseView = [[UIView alloc] init];
//        self.mTableView = [[UITableView alloc] init];
        self.mTableView = [[UITableView alloc] initWithFrame:CGRectMake(r.origin.x, r.origin.y + r.size.height, r.size.width,0) style:UITableViewStylePlain];
    }
    self.mTableView.allowsSelection = YES;
    self.mTableView.allowsSelectionDuringEditing = YES;
    
//        self.mTableBaseView.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y + self.frame.size.height , self.frame.size.width, self.mSuperView.frame.size.height - self.frame.origin.y - self.frame.size.height);
//    
//        self.mTableBaseView.backgroundColor = [UIColor redColor];
//        
//        UITapGestureRecognizer *bgTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgTappedAction:)];
//    
//        [self.mTableBaseView addGestureRecognizer:bgTap];
    
    
        self.mTableView.frame = self.frame;
        [Common removeExtraCellLines:self.mTableView];
        self.mTableView.delegate = self;
        self.mTableView.dataSource = self;
    
    CGRect rect = self.mTableView.frame;
    self.mTableView.frame = rect;
    [self.mSuperView addSubview:self.mTableView];
    
    //动画设置位置
    NSInteger n = [self.dropDownDataSource numberOfRowsInSection:self.numButton];
    if(n >= 5){
//        if(self.forScreenFlag){
//    rect.size.height = 175 * 3;
//    }
//        else{
        rect.size.height = 175;
     //   }
    }
    else{
        self.mTableView.scrollEnabled = NO;
//        if(self.forScreenFlag){
//           rect.size.height = HeighCell*n* 3;
//        }
//        else{
          rect.size.height = HeighCell*n;
   //     }
    }
    
    CGFloat imWidth = (Screen_Width-28)/(self.screenWidth/self.screenHeight);
    if(!self.doubleClickflag){
        imWidth = 354;
    }
    
//    if(self.forScreenFlag){
//    if((rect.origin.y+rect.size.height) > imWidth *3 ){
//        rect.origin.y = imWidth *3 - rect.size.height;
//    }
//    if((rect.origin.x+self.frame.size.width) >= (Screen_Width-28)*3){
//        rect.origin.x = (Screen_Width-28)*3 - self.frame.size.width;
//    }
//    }
//    else{
        if((rect.origin.y+rect.size.height) > imWidth){
            rect.origin.y = imWidth - rect.size.height;
        }
        if((rect.origin.x+self.frame.size.width) >= (Screen_Width-28)){
            rect.origin.x = (Screen_Width-28) - self.frame.size.width;
        }
  //  }
    if(self.flagNumList == 2){
        float y = self.frame.origin.y;
        rect.origin.y = y;
        self.mTableView.frame = rect;
    }
    else{
        self.mTableView.frame = rect;
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        self.mTableBaseView.alpha = 0.2;
        self.mTableView.alpha = 0.2;
        
        self.mTableBaseView.alpha = 1.0;
        self.mTableView.alpha = 1.0;
        self.mTableView.frame =  rect;
    }];
    [self.mTableView reloadData];
}

-(void)bgTappedAction:(UITapGestureRecognizer *)tap
{
    UIImageView *currentIV = (UIImageView *)[self viewWithTag:(SECTION_IV_TAG_BEGIN + currentExtendSection)];
    if(self.nomanage == 1){
    [UIView animateWithDuration:0.3 animations:^{
        if(self.tag == 0){
            CGRect r = currentIV.frame;
            r.origin.y = r.origin.y+5;
            currentIV.frame = r;
            currentIV.transform = CGAffineTransformRotate(currentIV.transform, DEGREES_TO_RADIANS_BAN(-90));
        }
        else if(self.tag == 1){
            currentIV.transform = CGAffineTransformRotate(currentIV.transform, DEGREES_TO_RADIANS(180));
        }

    }];
    }
    if(self.tag == 0){
        if ([self.dropDownDelegate respondsToSelector:@selector(moveViewBtn:)]) {
            [self.dropDownDelegate moveViewBtn:currentExtendSection];
        }
    }
    [self hideExtendedChooseView];
}

- (void)btnAction:(UIButton *)sender {
    if ([self.dropDownDelegate respondsToSelector:@selector(chooseAtSection:index:)]) {
        NSString *chooseCellTitle = [self.dropDownDataSource titleInSection:self.numButton index:sender.tag];
        UIButton *currentSectionBtn = (UIButton *)[self viewWithTag:SECTION_BTN_TAG_BEGIN + currentExtendSection];
        UILabel *lab = (UILabel *)[currentSectionBtn viewWithTag:SECTION_BTN_TAG_BEGIN+30];
        if(self.tag == 0){
            
            lab.text = chooseCellTitle;
        }
        else if(self.tag == 1){
            [currentSectionBtn setTitle:chooseCellTitle forState:UIControlStateNormal];
            currentSectionBtn.titleLabel.font = Font(16);
            currentSectionBtn.titleLabel.textColor = [Common colorFromHexRGB:@"333333"];
        }
        [self.dropDownDelegate chooseAtSection:self.numButton index:sender.tag];
        UIImageView *currentIV = (UIImageView *)[self viewWithTag:(SECTION_IV_TAG_BEGIN + currentExtendSection)];
        if(self.nomanage == 1){
            [UIView animateWithDuration:0.3 animations:^{
                if(self.tag == 0){
                    CGRect r = currentIV.frame;
                    r.origin.y = r.origin.y+5;
                    currentIV.frame = r;
                    currentIV.transform = CGAffineTransformRotate(currentIV.transform, DEGREES_TO_RADIANS_BAN(-90));
                }
                else if(self.tag == 1){
                    currentIV.transform = CGAffineTransformRotate(currentIV.transform, DEGREES_TO_RADIANS(180));
                }
                
            }];
        }
        if(self.tag == 0){
            if ([self.dropDownDelegate respondsToSelector:@selector(moveViewBtn:)]) {
                [self.dropDownDelegate moveViewBtn:currentExtendSection];
            }
        }
        
        [self hideExtendedChooseView];
    }
}
#pragma mark -- UITableView Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if(self.forScreenFlag){
//    return HeighCell * 3;
//    }else{
    return HeighCell;
   // }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.dropDownDelegate respondsToSelector:@selector(chooseAtSection:index:)]) {
        NSString *chooseCellTitle = [self.dropDownDataSource titleInSection:self.numButton index:indexPath.row];
        UIButton *currentSectionBtn = (UIButton *)[self viewWithTag:SECTION_BTN_TAG_BEGIN + currentExtendSection];
        UILabel *lab = (UILabel *)[currentSectionBtn viewWithTag:SECTION_BTN_TAG_BEGIN+30];
        if(self.tag == 0){
            
            lab.text = chooseCellTitle;
        }
        else if(self.tag == 1){
        [currentSectionBtn setTitle:chooseCellTitle forState:UIControlStateNormal];
        currentSectionBtn.titleLabel.font = Font(16);
        currentSectionBtn.titleLabel.textColor = [Common colorFromHexRGB:@"333333"];
        }
        [self.dropDownDelegate chooseAtSection:self.numButton index:indexPath.row];
        UIImageView *currentIV = (UIImageView *)[self viewWithTag:(SECTION_IV_TAG_BEGIN + currentExtendSection)];
        if(self.nomanage == 1){
            [UIView animateWithDuration:0.3 animations:^{
                if(self.tag == 0){
                    CGRect r = currentIV.frame;
                    r.origin.y = r.origin.y+5;
                    currentIV.frame = r;
                    currentIV.transform = CGAffineTransformRotate(currentIV.transform, DEGREES_TO_RADIANS_BAN(-90));
                }
                else if(self.tag == 1){
                    currentIV.transform = CGAffineTransformRotate(currentIV.transform, DEGREES_TO_RADIANS(180));
                }
                
            }];
        }
        if(self.tag == 0){
            if ([self.dropDownDelegate respondsToSelector:@selector(moveViewBtn:)]) {
                [self.dropDownDelegate moveViewBtn:currentExtendSection];
            }
        }

       [self hideExtendedChooseView];
    }
}

#pragma mark -- UITableView DataSource


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dropDownDataSource numberOfRowsInSection:self.numButton];
}

static NSString *cellID = @"CELLID";
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    cell.backgroundColor = [UIColor blackColor];
    tableView.separatorStyle = NO;
    UIView *viv = [cell viewWithTag:2014+indexPath.row];
    [viv removeFromSuperview];
    UIView *v = [[UIView alloc] init];
    CGRect r = cell.frame;
//    if(self.forScreenFlag){
//    r.size.height = HeighCell * 3;
//    r.size.width = self.frame.size.width;
//    }
//    else{
        r.size.height = HeighCell;
        r.size.width = self.frame.size.width;
   // }
    v.frame = r;
    v.tag = 2014+indexPath.row;
    v.backgroundColor = [UIColor grayColor];
    UILabel *lab = [[UILabel alloc] initWithFrame:v.frame];
    if(self.natureNum){
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.backgroundColor = [Common colorFromHexRGB:@"020f0f"];
        self.backgroundColor = [UIColor blackColor];
        self.mTableView.backgroundColor = [UIColor blackColor];
        self.mTableView.alpha = 0.8;
//        if(self.forScreenFlag)
//            cell.textLabel.font = [UIFont systemFontOfSize:17.0 * 3];
//        else{
            cell.textLabel.font = [UIFont systemFontOfSize:17.0];
     //   }
        cell.textLabel.text = [self.dropDownDataSource titleInSection:self.numButton index:indexPath.row];
    }
    else{
        cell.textLabel.textColor = [Common colorFromHexRGB:@"000000"];
        cell.textLabel.backgroundColor = [UIColor grayColor];
        cell.backgroundColor = [UIColor grayColor];
        self.backgroundColor = [UIColor grayColor];
        self.mTableView.backgroundColor = [UIColor grayColor];
//        if(self.forScreenFlag)
//            lab.font = [UIFont systemFontOfSize:17.0 * 3];
//        else{
            lab.font = [UIFont systemFontOfSize:17.0];
//        }
        lab.text = [self.dropDownDataSource titleInSection:self.numButton index:indexPath.row];
        [v addSubview:lab];
        [cell addSubview:v];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = cell.bounds;
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        btn.tag = indexPath.row;
        btn.backgroundColor = [UIColor clearColor];
        [cell addSubview:btn];
    }
    return cell;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
